package zhoufu.sz.sanxiao_check.ui.fragment;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.cy.src.update.download.Config;
import com.cy.src.update.ui.UpdateTask;
import java.io.File;
import zhoufu.sz.sanxiao_check.App;
import zhoufu.sz.sanxiao_check.R;
import zhoufu.sz.sanxiao_check.config.Constant;
import zhoufu.sz.sanxiao_check.databinding.FragmentUserCenterBinding;
import zhoufu.sz.sanxiao_check.inject.components.LoginComponet;
import zhoufu.sz.sanxiao_check.models.User;
import zhoufu.sz.sanxiao_check.ui.ModifyPasswordActivity;


/**
 * User:RCP
 * email:ruancaipu@gmail.com
 * Date:2016/7/20
 * 个人中心
 */
public class PersonCenterFragment extends BaseFragment implements View.OnClickListener
{

    FragmentUserCenterBinding _binding;

    @Nullable @Override public View onCreateView(LayoutInflater inflater,
            @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        LoginComponet componet = getComponent(LoginComponet.class);
        _binding = DataBindingUtil
                .inflate(inflater, R.layout.fragment_user_center, container, false,
                        componet);
        _binding.setUser(User.readUser());
        _binding.setOnClickLintener(this);
        return _binding.getRoot();
    }

    @Override public void onClick(View v)
    {
        super.onClick(v);
        switch (v.getId())
        {
            case R.id.usercenter_exitlogin:
                App.getApp().exit();
                break;
            case R.id.tv_update:
                update();
                break;
            case R.id.mTvUpdatePass:
                startActivity(new Intent(mAct, ModifyPasswordActivity.class));
                break;
        }
    }

    private void update()
    {
        Config config = new Config();
        config.setUpdateUrl(Constant.UPDATE_URL);
        config.setSilenceUpdate(false);//是否静默请求更新
        File file = new File(Environment.getExternalStorageDirectory(),
                mAct.getPackageName() + ".apk");
        config.setLocalDownloadFile(file);
        new UpdateTask(mAct, config).startRequest();
    }
}
